package m.fun.action;

import m.base.action.BaseAction;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.*;
import m.fun.model.AskDetail;
import m.fun.model.AskInfo;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.ListButton;
import m.html.list.ListElement;
import m.html.list.SuccessMethod;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

@ActionMeta(name = "fun/askDetail")
public class AskDetailAction extends BaseAction {
    private AskDetail model;
    @RequestMeta(logTitle = "询价明细",logDescription = "保存询价明细")
    public JSONMessage doSave() throws Exception {
        ModelCheckUtil.checkNotNull(model,"record.oid");
        AskDetail detail=ModelQueryList.getModel(AskDetail.class,model.getOid(),"ask.user.oid");
        PowerUtil.verifyUserData(detail.getAsk().getUser().getOid());
        ModelUpdateUtil.updateModel(model,"record.oid");
        return JSONMessage.success("保存成功");
    }
    public VueResult toEdit() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","goods.*","record.*");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        ModelForm form = new ModelForm("商品需求");
        form.addItem(
            FormHtml.inputStyle("展示名称", "{{model.title}}").setSpan(2),
            FormHtml.inputStyle("展示价格", "{{model.priceTxt}}"),
            FormHtml.inputStyle("商品名称", "{{model.goods.name}}").setSpan(2),
            FormHtml.inputStyle("数量", "{{model.num}}"),
            FormInput.selectOneModel("库存记录", "model","record","'action/store/goodsStockRecord/selectStockList?params[virtualTab]=A&params[goods.oid]='+model.goods.oid",
                    "<template v-if=\"model.record.oid\">{{null==model.record.surplusNum?'虚拟库存：需调货；':('剩余数量：'+model.record.surplusNum+'；'+(model.record.surplusNum<model.num?'库存不足':''))}}</template>").setSpan(3)
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    /*
     * 需求商品列表
     */
    public VueResult detailList() throws Exception {
        String askOid=getParamsValue("ask.oid","X");
        AskInfo ask=ModelQueryList.getModel(AskInfo.class,askOid,"*");
        if(null==ask) throw new MException(this.getClass(),"询价记录不存在");
        VueListResult result=getVueListResult("需求商品", AskDetail.class,actionUrl("detailList"));
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("展示名称", "title", 130, true));
        result.addCol(toTextColumn("展示价格", "priceTxt", 100, true).setAlign(Align.RIGHT));
        result.addCol(toIntegerColumn("需求数量", "num", 100, true));
        result.addCol(toTextColumn("商品分类", "goods.storeType.name", 100, true));
        result.addCol(toTextColumn("商品名称", "goods.name", 130, true));
        result.addCol(toNumberColumn("报价", "goods.offerPrice", 100, true));
        result.addCol(toNumberColumn("均价", "goods.surplusPrice", 100, true).setGroupTitle("剩余"));
        result.addCol(toIntegerColumn("数量", "goods.surplusNum", 100, true).setGroupTitle("剩余"));
        if(AskInfo.Build_No.equals(ask.getBuildStatus())) {
            result.addQueryFields("record.oid","record.surplusNum");
            result.addCol(toSpaceColumn("选择库存", 100,
                ListElement.test("record.record.oid",
                    ListElement.test("record.record.surplusNum!=null",
                        ListButton.modal("余{{record.record.surplusNum}}", ButtonType.DEFAULT, actionUrl("toEdit"), 900, SuccessMethod.REFRESH)
                            .addParam("model.oid", "oid")
                    ),
                    ListElement.elseOther(
                        ListButton.modal("需调货", ButtonType.GHOST, actionUrl("toEdit"), 900, SuccessMethod.REFRESH)
                            .addParam("model.oid", "oid")
                    )
                ),
                ListElement.elseOther(
                    ListButton.modal("选择库存", ButtonType.ERROR, actionUrl("toEdit"), 900, SuccessMethod.REFRESH)
                        .addParam("model.oid", "oid")
                )
            ).setFixed(Align.RIGHT));
        }
        //排序 result.addOrder
        //查询条件 result.addCondition
        result.addCondition(QueryCondition.eq("ask.oid",getParamsValue("ask.oid","X")));
        result.addOrder(QueryOrder.asc("oid"));
        return result;
    }

    public AskDetail getModel() {
        return model;
    }

    public void setModel(AskDetail model) {
        this.model = model;
    }
}
